import axios from 'axios'

const instance = axios.create({
    baseURL: '/api',
    timeout: 3000,
})

// 请求拦截器
instance.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token');
        if (token) {
            config.headers[`Authorization`] = `${token}` // 将 token 添加到请求头
            console.log('Authorization Header:', config.headers['Authorization']);
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
//登录接口
export function Login(LoginDTO) {
    return instance.request({
        url: '/user/login',
        method: 'POST',
        data: LoginDTO,
    })
}

// 登出接口
export function LogOut(Token) {
    return instance.request({
        url: '/user/logout',
        method: 'POST',
        data: Token,
    })
}

// 注册接口
export function Register(RegisterDTO) {
    return instance.request({
        url: '/user/register',
        method: 'POST',
        data: RegisterDTO,
    })
}

//查询菜品信息
export function GetDish(dishDTO) {
    return instance.request({
        url: '/dishes/getDishes',
        method: 'GET',
        data: dishDTO,
    })
}

// 获取用户信息
export function GetUserInfo() {
    return instance.request({
        url: '/user/getUserInfo',
        method: 'GET',
    })
}

// 更新用户信息
export function UpdateUserInfo(UserDTO) {
    return instance.request({
        url: '/user/updateUserInfo',
        method: 'POST',
        data: UserDTO,
    })
}

//根据条件查询菜品
export function GetDishByCondition(type, name) {
    return instance.request({
        url: '/dishes/getDishes',
        method: 'GET',
        params: {
            type: type,
            name: name
        }
    })
}

// 获取帖子列表
export function GetPosts() {
    return instance.request({
        url: '/community/getPost',
        method: 'GET',
    })
}

// 获取帖子详情
export function GetPost(id) {
    return instance.request({
        url: `/community/getPostById/${id}`, // 直接在路径中传递 postId
        method: 'GET'
    })
}

export function GetImage(url){
    return instance.request({
        url: url,
        method: 'GET',
    })
}

//发布帖子
export function AddPost(formData ){
    return instance.request({
        url: '/community/addPost',
        method: 'POST',
        data: formData ,
        headers: {
            'Content-Type': 'multipart/form-data', // 设置请求头
        },
    })
}

//删除帖子
export function DeletePost(postId){
    return instance.request({
        url: `/manage/deletePost?postId=${postId}`,
        method: 'DELETE',
    })
}

//发布评论
export function AddComment(CommentDTO){
    return instance.request({
        url: '/community/addComment',
        method: 'POST',
        data: CommentDTO,
    })
}

//获取订单列表
export function GetOrders(page, size){
    return instance.request({
        url: `/order/getOrderList`,
        method: 'GET',
        params: {
            page: page,
            size: size
        }
    })
}

//管理员获取全部订单
export function GetAllOrders(page, size){
    return instance.request({
        url: `/manage/getOrder`,
        method: 'GET',
        params: {
            page: page,
            size: size
        }
    })
}

//查看订单详情
export function GetOrderDetail(orderId){
    return instance.request({
        url: `/order/getOrderById`,
        method: 'GET',
        params: {
            orderId: orderId
        }
    })
}

//支付订单
export function PayOrder(orderId) {
    return instance.request({
        url: `/canteen/pay?orderId=${orderId}`,
        method: 'POST'
    })
}

//删除订单
export function DeleteOrder(ids){
    return instance.request({
        url: `/order/deleteOrdersById`,
        method: 'DELETE',
        params: {
            ids: ids
        }
    })
}

//获取店铺列表
export function GetShops(page, size){
    return instance.request({
        url: `/canteen/getCanteen`,
        method: 'GET',
        params: {
            page: page,
            size: size
        }
    })
}

//根据店铺id获取店铺详情
export function GetCanteenDetail(canteenId){
    return instance.request({
        url: `/canteen/getCanteenById`,
        method: 'GET',
        params: {
            canteenId: canteenId
        }
    })
}

//查询店铺餐桌信息
export function GetCanteenTable(canteenId){
    return instance.request({
        url: `/canteen/getSeatList`,
        method: 'GET',
        params: {
            canteenId: canteenId
        }
    })
}

//预约餐桌
export function BookTable(seatId, status){
    return instance.request({
        url: `/canteen/checkSeat`,
        method: 'POST',
        params: {
            seatId: seatId,
            status: status
        }
    })
}

//编辑餐桌
export function UpdateSeat(seatId, capacity, price, number, status){
    return instance.request({
        url: `/manage/updateSeat`,
        method: 'POST',
        params: {
            seatId: seatId,
            capacity: capacity,
            price: price,
            number: number,
            status: status
        }
    })
}

//删除位置
export function DeleteSeat(seatId){
    return instance.request({
        url: `/manage/deleteSeat`,
        method: 'DELETE',
        params: {
            seatId: seatId
        }
    })
}

//添加餐桌
export function AddSeat(canteenId, seatNum, capacity, price){
    return instance.request({
        url: `/manage/addSeat`,
        method: 'POST',
        params: {
            canteenId: canteenId,
            seatNum: seatNum,
            capacity: capacity,
            price: price,
        }
    })
}

//查询餐厅菜品列表
export function GetDishes(canteenId){
    return instance.request({
        url: `/canteen/getDishesByCanteenId`,
        method: 'GET',
        params: {
            canteenId: canteenId
        }
    })
}

//修改菜品信息
export function UpdateDish(formData){
    return instance.request({
        url: `/manage/updateDishes`,
        method: 'POST',
        data: formData
    })
}

//获取菜品类型
export function GetDishType(){
    return instance.request({
        url: `/manage/getType`,
        method: 'GET',
    })
}

//添加菜品
export function AddDish(formData){
    return instance.request({
        url: `/manage/addDishes`,
        method: 'POST',
        data: formData,
    })
}

//删除菜品
export function DeleteDish(dishId){
    return instance.request({
        url: `/manage/deleteDishes`,
        method: 'DELETE',
        params: {
            dishId: dishId
        }
    })
}

//查看餐厅评价
export function GetReview(canteenId){
    return instance.request({
        url: `/community/getReview`,
        method: 'GET',
        params: {
            canteenId: canteenId
        }
    })
}

//添加餐厅评价
export function AddReview(formData){
    return instance.request({
        url: `/community/addReview`,
        method: 'POST',
        data: formData,
        // headers: {
        //     'Content-Type': 'multipart/form-data', // 设置请求头
        // },
    })
}

//删除餐厅评价
export function DeleteReview(reviewId){
    return instance.request({
        url: `/manage/deleteReview`,
        method: 'DELETE',
        params: {
            reviewId: reviewId
        }
    })
}

// 获取用户列表
export function getUserList() {
    return instance.request({
        url: '/manage/getUser',
        method: 'GET',
    });
}

// 修改用户权限
export function updateUser(userId, type) {
    return instance.request({
        url: '/manage/updateUser',
        method: 'POST',
        params:{
            userId : userId,
            type : type
        }
    });
}

// 删除用户
export function deleteUser(userId) {
    return instance.request({
        url: '/manage/deleteUser',
        method: 'DELETE',
        params:{
            userId : userId
        }
    });
}

// 获取餐厅列表
export function getCanteenList() {
    return instance.request({
        url: '/manage/getCanteen',
        method: 'GET',
    });
}

// 修改餐厅信息
export function updateCanteen(id, name, position) {
    return instance.request({
        url: '/manage/updateCanteen',
        method: 'POST',
        params:{
            id : id,
            name : name,
            position : position
        }
    });
}

// 添加餐厅
export function addCanteen(name, position) {
    return instance.request({
        url: '/manage/addCanteen',
        method: 'POST',
        params:{
            name : name,
            position : position
        }
    });
}

// 删除餐厅
export function deleteCanteen(canteenId) {
    return instance.request({
        url: '/manage/deleteCanteen',
        method: 'DELETE',
        params:{
            canteenId : canteenId
        }
    });
}

//点赞帖子
export function likePost(postId){
    return instance.request({
        url: `/community/likePost/${postId}`,
        method: 'POST',
    })
}

// 查看当前用户是否点赞帖子
export function isLike(postId) {
    return instance.request({
        url: `/community/isLike/${postId}`, // 将 postId 放在 URL 路径中
        method: 'GET',
    });
}

//删除帖子评论
export function deleteComment(commentId){
    return instance.request({
        url: `/manage/deleteComment`,
        method: 'DELETE',
        params: {
            commentId: commentId
        }
    })
}

//根据餐厅名模糊查询店铺列表
export function getCanteenByName(name){
    return instance.request({
        url: `/manage/getCanteenByName`,
        method: 'GET',
        params: {
            name: name
        }
    })
}

//根据用户名模糊查询用户列表
export function getUserByName(name){
    return instance.request({
        url: `/manage/getUserByName`,
        method: 'GET',
        params: {
            name: name
        }
    })
}

//支付宝沙箱
export function reqAliPay(orderId) {
    return instance.request({
        url: `/alipay/pay`, //https://openapi-sandbox.dl.alipaydev.com/gateway.do
        method: 'POST',
        params: {
            orderId: orderId
        },
        responseType: 'text' // 关键：确保接收原始响应而不是JSON
    })
}
